package org.wmine.algo;

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;


public class DT
{
	Vector superSet;
	Vector rules;
	Vector gotBit;
	Vector decision;
	int goalIndex;int rowCnt;
	String [][] data4;double [] gains;
	Vector vName;
	public JPanel getDT(final String[]attribs,int colknt,final int cntRow,String [][] data,int[]selected,String classLbl)
	{
		//JPanel jp=new JPanel();
		int ind=0;
		rowCnt=cntRow;
		data4=data;
		superSet=new Vector();
		rules=new Vector();
		gotBit=new Vector();
		decision=new Vector();
		vName=new Vector();
		
		for(int i=0;i<selected.length;i++)
		{
			if(selected[i]==1)
				//	think whether to have set of indices or that of labels
				superSet.insertElementAt(i,ind++);	
			else if(selected[i]==2)
				goalIndex=i;
		}
		vName=classify(goalIndex);
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>"+superSet.size()+"Goal"+goalIndex);
		for(int i=0;i<superSet.size();i++)
		{
			System.out.println(superSet.elementAt(i));
		}
		System.out.println("Above commented out!");
		//String t="a=b,c=d,e=f";	buildTree(t,-1);
	//	while(true)
	//	{
			if(rules.size()==0)
				buildTree(null,-1);
			//else
			//{
				int pending;
				while((pending=allzeros())!=-1)
				{
					System.out.println("Iterating!!!"+pending);
					buildTree(rules.elementAt(pending).toString(),pending);
					for(int i=0;i<rules.size();i++)
					{
						System.out.print(rules.elementAt(i).toString());
						System.out.print(" >> ");
						System.out.print(gotBit.elementAt(i).toString());
						if(Integer.parseInt(gotBit.elementAt(i).toString())==1)
						System.out.println(decision.elementAt(i).toString());
					}
				}
				System.out.println("FINISHED!!!");
				for(int i=0;i<rules.size();i++)
				{
					System.out.print(rules.elementAt(i).toString());
					System.out.print(" >> ");
					System.out.print(decision.elementAt(i).toString());
				}
				
				//JTreePanel jp = null;
				 JPanel p=SimpleTreeFrame(attribs,rules,decision);
				 p.setBounds(10,30,4200,410);
				 if(p==null) System.out.print("leave it");
				return p;
				//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			      //frame.show();
		//		break;
		//	}
		//}
	}
	public int allzeros()
	{
		int flag=-1;
		for(int i=0;i<rules.size();i++)
		{
			if(Integer.parseInt(gotBit.elementAt(i).toString())==0)
			{
				flag=i;//still to branch!
				break;
			}
		}
		System.out.println(rules.size());
		return flag;
	}
	
	public void buildTree(String rule,int rulePtr)
	{
		if(rule==null)//start of building it
		{
			Vector temp=findEntropy(rule);
			if(temp.elementAt(0).equals(0))
			{
				/*//all the tuples in the complete database have same label
				 //System.out.println("**************************"+temp.elementAt(1));*/ 
			}
			else
			{
				//System.out.println("///////////////////////////////////////////////////////");
				int minAttribInd=findGains(rule);
				System.out.println((minAttribInd)+">>>>>>><<<<<<<<<<<<<<");
				//3 vectors willbe empty at this time
				Vector v1=classify(minAttribInd);
				rulePtr=0;
				for(int i=0;i<v1.size();i++)
				{
					String s=""+minAttribInd+"="+v1.elementAt(i).toString();
					rules.insertElementAt(s,rulePtr);
					gotBit.insertElementAt(0,rulePtr);
					System.out.println(s);
				}
				
			}
		}
		else//means you have already started building it
		{
			System.out.println("before temp");
			Vector temp=findEntropy(rule);
			System.out.println("after temp");
			if(temp.elementAt(0).equals(0))
			{
				System.out.println("zero entropy");
				//all the tuples in the complete database have same label
				gotBit.insertElementAt(1,rulePtr);
				decision.insertElementAt(temp.elementAt(1),rulePtr);
				//System.out.println(temp.elementAt(1)); 
			}
			else
			{
				System.out.println("non zero entropy");
				int minAttribInd=findGains(rule);
				/******************/
				System.out.println(minAttribInd);
				Vector query=new Vector();
				StringTokenizer st=new StringTokenizer(rule);
				int tokenKnt=0;	Vector tokens=new Vector();	int ind=0;
				while(st.hasMoreElements())
				{
					String s=st.nextToken(",");
					StringTokenizer st1=new StringTokenizer(s);
					int i=0;
					while(st1.hasMoreElements())
					{
						String s1=st1.nextToken("=");
						tokens.insertElementAt(s1,tokenKnt++);
						if(i==0)
						{query.insertElementAt(s1,ind++);i=1;}
					}
				}
				//System.out.println("Vector length"+tokens.size()+"query count"+query.size());
				/********************/
				Vector v1=classifyQuery(minAttribInd,tokens);
				String s1=rules.elementAt(rulePtr).toString();
				String s2=s1+","+minAttribInd+"="+v1.elementAt(0).toString();
				rules.removeElementAt(rulePtr);
				rules.insertElementAt(s2,rulePtr);
				int index=rules.size();
				for(int i=1;i<v1.size();i++)
				{
					String s3=s1+","+minAttribInd+"="+v1.elementAt(i).toString();
					rules.insertElementAt(s3,index);
					gotBit.insertElementAt(0,index++);
				}
				
			}
		}
	}
	public Vector classify(int index)
	{	
		//System.out.println("in classssssssssssssify!!!!!!!!"+index);
		int size=0;	int ind;Vector vt=new Vector();
		for(int o=0;o<rowCnt;o++)
		{
			if(vt.size()!=0)
			{
				if((ind=vt.indexOf(data4[o][index]))==-1)	
				{
					vt.insertElementAt(data4[o][index], size);
					size++;
				}
			}
			else
			{
				vt.insertElementAt(data4[o][index], size);
				size++;
			}			
		}
		System.out.println("No.of Classes:"+vt.size());
		//for(int y=0;y<vt.size();y++)
		//System.out.println("Class "+(y+1)+":"+vt.get(y));
		//if(index==goalIndex)
		//System.out.println("????????????????????????????????"+vt.size());
		return vt;
	}
	public int findGains(String rule)
	{
		//superSet and the first part of the rule will have indices only!!
		// goalIndex has the index of the goal attribute
		int p=0;
		//double val=1;
		System.out.println(rule);
		int [][]mat;int[]rowSum;int total=0;
		//int []remainingInd;
		Vector remainingInd=new Vector();
		if(rule==null)//find gains of all those in the superSet
		{
			gains=new double[superSet.size()];
			for(int i=0;i<superSet.size();i++)
			{
				//val=1;
				System.out.println(Integer.parseInt(superSet.elementAt(i).toString()));
				Vector tempV=classify(Integer.parseInt(superSet.elementAt(i).toString()));
				mat=new int[tempV.size()][vName.size()];
				rowSum=new int[tempV.size()];
				for(int r=0;r<tempV.size();r++)
				{
					int rsum=0;
					for(int c=0;c<vName.size();c++)
					{
						int cnt=0;
						for(int rr=0;rr<rowCnt;rr++)
						{
							if(data4[rr][Integer.parseInt(superSet.elementAt(i).toString())].equals(tempV.elementAt(r))&&data4[rr][goalIndex].equals(vName.elementAt(c)))
							{
								cnt++;
								//System.out.println("Count:"+cnt);
							}
						}
						mat[r][c]=cnt;
						//System.out.println(mat[r][c]);
						rsum+=cnt;
					}
					rowSum[r]=rsum;
					total+=rsum;
				}
				double ans=0;
				//System.out.println(vName.size()+"/././././//./.");
				for(int r=0;r<tempV.size();r++)
				{
					double sum=0;
					for(int c=0;c<vName.size();c++)
					{
						//System.out.println("this one!!"+rowSum[r]);
						/**********************comment it out**************************/
						sum+=(((double)mat[r][c]/(double)rowSum[r])*(log2((double)mat[r][c]/(double)rowSum[r]))*(-1));
						
					}	
					//System.out.println("Sum!!"+sum);
					ans+=(((double)rowSum[r]/(double)total)*sum);
				}
				gains[i]=ans;
				System.out.println(gains[i]+"##################################");
			}
			return Integer.parseInt(superSet.elementAt(findMin()).toString());
		}
		/*********************************************************/
		else//need to fire the query!
		{
			//System.out.println(Integer.parseInt(superSet.elementAt(i).toString()));
			Vector query=new Vector();
			
			StringTokenizer st=new StringTokenizer(rule);
			int tokenKnt=0;	Vector tokens=new Vector();	int ind=0;
			//query=new Vector();
			while(st.hasMoreElements())
			{
				String s=st.nextToken(",");
				StringTokenizer st1=new StringTokenizer(s);
				int i=0;
				while(st1.hasMoreElements())
				{
					String s1=st1.nextToken("=");
					System.out.println(s1);
					tokens.insertElementAt(s1,tokenKnt++);
					if(i==0)
					{query.insertElementAt(s1,ind++);i=1;}
				}
			}
			System.out.println("query count"+query.size()+query.elementAt(0)+">>"+query.indexOf(0));
			int att=superSet.size()-query.size();
			//System.out.print("Attttttttttttt"+att);
			//System.out.print("Attttttttttttt"+att);
			gains=new double[att];////////*********************************
			//remainingInd=new int[att];////********************
			
			//////////////inintializing remainingInd/////////////////////////
			int jRem=0;
			//for(int i=0;i<superSet.size();i++)
			//System.out.println(i+"] "+superSet.elementAt(i).toString());
			//for(int i=0;i<query.size();i++)
			//System.out.println(i+"] "+query.elementAt(i).toString());
			/****************************************
			  
			for(int i=0;i<superSet.size();i++)
			{
				if(query.indexOf(superSet.elementAt(i))==-1)
				{
					System.out.println("remainingg"+remainingInd.size());
					//remainingInd[jRem++]=
					//System.out.println(Integer.parseInt(superSet.elementAt(i).toString()));
					remainingInd.insertElementAt(superSet.elementAt(i),jRem++);
				}
								
			}
			**************************************************/
			//query contains indices on superset and not actual attribute index in the table
			//superset conatins index to table
			int pointer=0;
				for(int j=0;j<superSet.size();j++)
				{
					int flag=0;
					for(int i=0;i<query.size();i++)
					{
						if(j==(Integer.parseInt(query.elementAt(i).toString())))
						{
							//dont include it into remaining
							flag=1;
						}
					}
					if(flag==0)//remaining
					{
						remainingInd.insertElementAt(superSet.elementAt(j),pointer);
						pointer++;
					}
				}
			for(int i=0;i<remainingInd.size();i++)
			  System.out.println(remainingInd.elementAt(i));
			////////////calcualtion of mean for the remaining ones after firing the query
			for(int i=0;i<gains.length;i++)
			{
				//	val=1;				
				Vector tempV=classifyQuery(Integer.parseInt(remainingInd.elementAt(i).toString()),tokens);//gives diff classes in itself
				mat=new int[tempV.size()][vName.size()];
				rowSum=new int[tempV.size()];
				for(int r=0;r<tempV.size();r++)
				{
					int rsum=0;
					for(int c=0;c<vName.size();c++)
					{
						int cnt=0;
						for(int rr=0;rr<rowCnt;rr++)
						{
							///////////
							int flag=0;
							for(int ii=0;ii<tokens.size();ii+=2)
							{
								if(data4[rr][Integer.parseInt(tokens.elementAt(ii).toString())].equals(tokens.elementAt(ii+1).toString()))
								{}
								else
									flag=1;
							}
							//////////
							if(flag==0)
							{
								if(data4[rr][Integer.parseInt(remainingInd.elementAt(i).toString())].equals(tempV.elementAt(r))&&data4[rr][goalIndex].equals(vName.elementAt(c)))
								{
									cnt++;
								}
							}	
						}
						mat[r][c]=cnt;
						System.out.println("==============================================>"+cnt);
						rsum+=cnt;
					}
					rowSum[r]=rsum;
					total+=rsum;
				}
				double ans=0;
				for(int r=0;r<tempV.size();r++)
				{
					double sum=0;
					for(int c=0;c<vName.size();c++)
					{
						if(mat[r][c]==0)
						{System.out.println("returning "+tempV.elementAt(r).toString()+" >> "+Integer.parseInt(remainingInd.elementAt(i).toString()));return Integer.parseInt(remainingInd.elementAt(i).toString());}
						sum+=(((double)mat[r][c]/(double)rowSum[r])*(log2((double)mat[r][c]/(double)rowSum[r]))*(-1));
					}		
					//if(sum==0)
					//{
						System.out.println("i goooooooooooooooooooooooooooooot it maaaaaaaaaaaaaaaaaaan");
					//}
					System.out.print(sum);
					ans+=(((double)rowSum[r]/(double)total)*sum);
					System.out.println("=="+ans);
				}
				gains[i]=ans;
				System.out.println("==============================================>>>>>>>>>>>>"+ans);
			}
			return findMin();
			/////////////////////////////////////////
		}
		/*********************************************************/
	}
	public Vector classifyQuery(int attribIndex,Vector strTok)
	{
		//2,0=senior
		int size=0;	int ind;Vector vt=new Vector();
		for(int o=0;o<rowCnt;o++)
		{
			if(vt.size()!=0)
			{
				if((ind=vt.indexOf(data4[o][attribIndex]))==-1)	
				{
					int flag=0;
					for(int i=0;i<strTok.size();i+=2)
					{
						if(data4[o][Integer.parseInt(strTok.elementAt(i).toString())].equals(strTok.elementAt(i+1).toString()))
						{}
						else
							flag=1;
					}
					if(flag==0)
					{
						vt.insertElementAt(data4[o][attribIndex], size);
						size++;
					}
				}
			}
			else
			{
				int flag=0;
				for(int i=0;i<strTok.size();i+=2)
				{
					if(data4[o][Integer.parseInt(strTok.elementAt(i).toString())].equals(strTok.elementAt(i+1).toString()))
					{}
					else
						flag=1;
				}
				if(flag==0)
				{
					vt.insertElementAt(data4[o][attribIndex], size);
					size++;
				}
			}			
		}
		System.out.println("No.of Classes:"+vt.size());
		for(int y=0;y<vt.size();y++)
		System.out.println("Class "+(y+1)+":"+vt.get(y));
		return vt;
	}
	public int findMin()
	{
		int minPtr=-1;
		if(gains.length!=0)
		{
			double min=gains[0];
			minPtr=0;
			for(int i=0;i<gains.length;i++)
			{
				if(min>gains[i])
				{
					min=gains[i];
					minPtr=i;
				}
			}
		}
		return minPtr;
	}
	public static double log2(double d) {
		return Math.log(d)/Math.log(2.0);
	}
	public Vector findEntropy(String rule)
	{
		Vector temp=new Vector();
		if(rule==null)
		{
			String match=data4[0][goalIndex];
			for(int i=0;i<rowCnt;i++)
			{
				if(data4[i][goalIndex].equals(match))
					;				
				else
				{
					temp.insertElementAt(1,0);
					return temp;
				}
			}
			temp.insertElementAt(0,0);
			temp.insertElementAt(match,1);
			return temp;
		}
		else
		{
			StringTokenizer st=new StringTokenizer(rule);
			int tokenKnt=0;
			Vector tokens=new Vector();
			while(st.hasMoreElements())
			{
				String s=st.nextToken(",");
				StringTokenizer st1=new StringTokenizer(s);
				while(st1.hasMoreElements())
				{
					String s1=st1.nextToken("=");
					tokens.insertElementAt(s1,tokenKnt++);						
				}
			}
			System.out.println("iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiVector length"+tokens.size());
			///////////////////////
			/*String match=data4[0][goalIndex];
			for(int i=0;i<rowCnt;i++)
			{
				if(data4[i][goalIndex].equals(match))
				{
					for(int k=0;k<tokens.size();k+=2)
					{
						if(data4[i][Integer.parseInt(tokens.elementAt(k).toString())].equals(tokens.elementAt(k+1)))
							;
						else
						{
							temp.insertElementAt(1,0);
							return temp;
						}						
					}
					temp.insertElementAt(0,0);
					temp.insertElementAt(match,1);
					return temp;
				}			
				else
				{
					temp.insertElementAt(1,0);
					return temp;
				}
			}
			temp.insertElementAt(0,0);
			temp.insertElementAt(match,1);
			return temp;
			*/
			
			
				//==============================================start
				int first=0,flag=0;String match="";
				for(int i=0;i<rowCnt;i++)
				{
					flag=0;
					for(int k=0;k<tokens.size();k+=2)
					{
						if(data4[i][Integer.parseInt(tokens.elementAt(k).toString())].equals(tokens.elementAt(k+1)))
							;
						else flag=1;						
					}
					if(flag==0)
					{
						if(first==0)
						{
							match=data4[i][goalIndex];
							first=1;
						}
						else
						if(data4[i][goalIndex].equals(match)) ;
						else
						{
							temp.insertElementAt(1,0);
							return temp;	
						}
							//{flg++;entered++;}
					}					
				}
				temp.insertElementAt(0,0);
				temp.insertElementAt(match,1);
				return temp;	
				///////////////end
	

			
			
			
			
			
			
			
			
			//////////////////////////			
		}
	}
	int h;
	Container contentPane;
	String []attribs;
	Vector decision1;
	//public JPanel getInputWindow()
	

   public JPanel SimpleTreeFrame(String[]attrib,Vector rules,Vector decision)
   {  
	   JPanel p=new JPanel();
	   attribs=attrib;
	   decision1=decision;
      //setTitle("SimpleTree");
      //setSize(WIDTH, HEIGHT);
      //contentPane = getContentPane();
      //classifyFirst()
      //===================
      int tokenKnt=0;	Vector tokens=new Vector();	int ind=0;
      for(int i=0;i<rules.size();i++)
      {
    		StringTokenizer st=new StringTokenizer(rules.elementAt(i).toString());
			//while(st.hasMoreElements())
			//{
				String s=st.nextToken(",");
				//StringTokenizer st1=new StringTokenizer(s);
				//int i=0;
				//while(st1.hasMoreElements())
				//{
					//String s1=st1.nextToken("=");
				int flg=0;
				for(int j=0;j<tokens.size();j++)
				{
					StringBuffer sb=new StringBuffer();
					StringTokenizer st1=new StringTokenizer(s);
					String b=st1.nextToken("=");
					String k=st1.nextToken();
					sb.append(attribs[Integer.parseInt(b)]);
					sb.append("=");
					sb.append(k);
					//tokens.insertElementAt(sb.toString(),tokenKnt++);
					String match=sb.toString();
					System.out.println(match+" Shreeeeeeeeeeeeeeeeeeeeee "+tokens.elementAt(j));
					if(tokens.elementAt(j).toString().equals(match))
					{
						flg=1;
						System.out.println("flg=1");
					}
				}
				if(flg==0)
				{
					StringBuffer sb=new StringBuffer();
					StringTokenizer st1=new StringTokenizer(s);
					String b=st1.nextToken("=");
					String k=st1.nextToken();
					sb.append(attribs[Integer.parseInt(b)]);
					sb.append("=");
					sb.append(k);
					System.out.println("flg=0"+sb.toString());
					tokens.insertElementAt(sb.toString(),tokenKnt++);
				}
					//if(i==0)
					//{query.insertElementAt(s1,ind++);i=1;}
				//}
				
			//}
      }
    	  //====================
      basicBranches=tokens.size();
      System.out.println("before building");
      buildTree(tokens);
      System.out.println("before building");
      //call("1=Youth","2=Shree");
      //call("1=Youthi","2=Shree");
      for(h=0;h<rules.size();h++)
      {
    	  String rule=rules.elementAt(h).toString();
    	  StringBuffer corrected=new StringBuffer();
    	  //////''''''''''''''''''''''''''
    	  	StringTokenizer st=new StringTokenizer(rule);
   	   		Vector v=new Vector();
   	   		int index=0;
   	   		while(st.hasMoreElements())
   	   		{
   	   			String s=st.nextToken(",");
   	   			StringTokenizer st1=new StringTokenizer(s);
   	   			String s1=st1.nextToken("=");
   	   			String s2=st1.nextToken();
   	   			String sok=attribs[Integer.parseInt(s1)]+"="+s2;
   	   			System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+sok);
   	   			//v.insertElementAt(sok,ind++);
   	   			corrected.append(sok);
   	   			if(st.hasMoreElements())corrected.append(",");
   	   		}
    	  
    	  //////////''''''''''''''''''''''
    	  //String sok1;
    	  callVector(corrected.toString());
      }
      tree.setBounds(10,30,4200,410);
      JScrollPane jsp1=new JScrollPane(tree);
    //  jsp1.setBounds(10,30,4200,410);
      p.setBounds(10,30,4200,410);
      p.add(jsp1);
      return p;
   }
   public int basicBranches;
   public void buildTree(Vector v)
   {
	   	/////StringTokenizer st1=new StringTokenizer(v.elementAt(0).toString());
		//int i=0;
	   	//while(st1.hasMoreElements())
	   	//{
			////String s1=st1.nextToken("=");
		//}
	   System.out.print("build treeeeeeeeeeeeeeeee");
	   	  root  = new DefaultMutableTreeNode("Root");
	   	  for(int k=0;k<v.size();k++)
	   	  {
	   		  country  = new DefaultMutableTreeNode(v.elementAt(k).toString());
	   		  root.add(country);
	   	  }  
	      tree = new JTree(root);
   }
   DefaultMutableTreeNode country;
   //////////
   
   
   public void callVector(String path)
   {
	   StringTokenizer st=new StringTokenizer(path);
	   Vector v=new Vector();
	   int ind=0;
	   while(st.hasMoreElements())
	   {
		   String s=st.nextToken(",");
		   //StringTokenizer st1=new StringTokenizer(s);
		   //while(st1.hasMoreElements())
		   //{
			 //  String s1=st1.nextToken("=");
//			   String s2=st1.nextToken();
	//		   String sok=attribs[Integer.parseInt(s1)]+"="+s2;
		//	   System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+sok);
			   v.insertElementAt(s,ind++);
		//   }
	   }
	   int got=0;int pathInd=0;
	   Enumeration e=root.breadthFirstEnumeration();
	   System.out.println(e.nextElement().toString());
	   for(int i=0;i<basicBranches&&got==0;i++)
	   {   
		   	  
		   DefaultMutableTreeNode node=(DefaultMutableTreeNode)e.nextElement();
		   System.out.println(node+","+v.elementAt(0).toString());
		   //????????????????????
		   if(node.getUserObject().toString().equals(v.elementAt(0).toString()))
		   {
			   
			   pathInd=1;
			   //Enumeration e1=node.pathFromAncestorEnumeration(root);
			   Enumeration e1=node.children();
			   System.out.println(node.isLeaf()+" "+node.getUserObject());
			   int flg=0;
			   //while(e1.hasMoreElements())
			   //{
				 //  DefaultMutableTreeNode node2=(DefaultMutableTreeNode)e1.nextElement();
//				   System.out.println("before while");
	//		   }  
			   if(node.isLeaf()&&node.getUserObject().toString().equals(v.elementAt(pathInd-1).toString()))
			   {
				   while(pathInd<v.size())
				   {
					   System.out.println("adding");
					   StringBuffer sb1=new StringBuffer();
					   String sss=v.elementAt(pathInd++).toString();
					   System.out.print(sss);
					   StringTokenizer st11=new StringTokenizer(sss);
					   Vector v1=new Vector();
					   int ind1=0;
					   while(st11.hasMoreElements())
					   {
						   String s=st11.nextToken(",");
						   StringTokenizer st1=new StringTokenizer(s);
						   while(st1.hasMoreElements())
						   {
							   String s1=st1.nextToken("=");
							   v1.insertElementAt(s1,ind1++);
						   }
					   }  
					   for(int i1=0;i1<v1.size();i1+=2)
					   {
						   //sb1.append(attribs[Integer.parseInt(v1.elementAt(i1).toString())]);
						   sb1.append(v1.elementAt(i1).toString());
						   sb1.append("=");
						   sb1.append(v1.elementAt(i1+1).toString());
					   }
					   node.add(new DefaultMutableTreeNode(sb1.toString()));
					   //node.add(new DefaultMutableTreeNode(v.elementAt(pathInd++)));
					   node=(DefaultMutableTreeNode)node.getChildAt(0);
					   System.out.println(node.isLeaf()+"+++++++++++++++++++");
				   }
				   node.add(new DefaultMutableTreeNode(decision1.elementAt(h)));
				   pathInd=0;
				   break;
			   }   	
			   else
				   callEnum(e1,v,pathInd);			   
			   
			   
			   
			   System.out.println("after while");
			 got=1;
		   }
		   //????????????????????
	   }
	}
   
 public void callEnum(Enumeration e1,Vector v,int pathInd)
 {
	  System.out.println("coming1");
	int flg=0;
	   while(e1.hasMoreElements())
	   {
		   System.out.println("coming2");
		   DefaultMutableTreeNode node1=(DefaultMutableTreeNode)e1.nextElement();
		   //node1.se
		   System.out.println(node1.getUserObject().toString()+" >>1");
		   System.out.println(v.elementAt(pathInd-1).toString()+">>2");
		   if(node1.isLeaf()&&node1.getUserObject().toString().equals(v.elementAt(pathInd-1).toString()))
		   {
			   while(pathInd<v.size())
			   {
				   System.out.println("adding");
				   ///////////////////////
				   StringBuffer sb1=new StringBuffer();
				   String sss=v.elementAt(pathInd++).toString();				   
				   StringTokenizer st=new StringTokenizer(sss);
				   Vector v1=new Vector();
				   int ind=0;
				   while(st.hasMoreElements())
				   {
					   String s=st.nextToken(",");
					   StringTokenizer st1=new StringTokenizer(s);
					   while(st1.hasMoreElements())
					   {
						   String s1=st.nextToken("=");
						   v1.insertElementAt(s1,ind++);
					   }
				   }  
				   for(int i=0;i<v1.size();i+=2)
				   {
					   sb1.append(v1.elementAt(i).toString());
					   sb1.append("=");
					   sb1.append(v1.elementAt(i+1).toString());
				   }
				   node1.add(new DefaultMutableTreeNode(sb1.toString()));
				   /////////////////////
				   node1=(DefaultMutableTreeNode)node1.getChildAt(0);
				   System.out.println(node1.isLeaf()+"+++++++++++++++++++");
				   flg=1;
			   }
			   node1.add(new DefaultMutableTreeNode(decision1.elementAt(h)));
			   pathInd=0;
			   break;
		   }
		   else
		   {
			   if((!node1.isLeaf())&&node1.getUserObject().toString().equals(v.elementAt(pathInd-1).toString()))
			   {
				   callEnum(node1.children(),v,pathInd);
			   }
			   else
				   if(!node1.getUserObject().toString().equals(v.elementAt(pathInd-1).toString()))
			   {
				   DefaultMutableTreeNode parent=(DefaultMutableTreeNode)node1.getParent();
				   parent.add(new DefaultMutableTreeNode(v.elementAt(pathInd++)));
				   
				   DefaultMutableTreeNode node2=(DefaultMutableTreeNode)parent.getChildAfter(node1);
				   //callEnum(node1.children(),v,pathInd);
				   while(pathInd<v.size())
				   {
					   //System.out.println("adding");
					   //node2.add(new DefaultMutableTreeNode(v.elementAt(pathInd++)));
					   StringBuffer sb1=new StringBuffer();
					   String sss=v.elementAt(pathInd++).toString();				   
					   StringTokenizer st=new StringTokenizer(sss);
					   Vector v1=new Vector();
					   int ind=0;
					   while(st.hasMoreElements())
					   {
						   String s=st.nextToken(",");
						   StringTokenizer st1=new StringTokenizer(s);
						   while(st1.hasMoreElements())
						   {
							   String s1=st.nextToken("=");
							   v1.insertElementAt(s1,ind++);
						   }
					   }  
					   for(int i=0;i<v1.size();i+=2)
					   {
						   sb1.append(v1.elementAt(i).toString());
						   sb1.append("=");
						   sb1.append(v1.elementAt(i+1).toString());
					   }
					   node2.add(new DefaultMutableTreeNode(sb1.toString()));
					   node2=(DefaultMutableTreeNode)node2.getChildAt(0);
					   System.out.println(node2.isLeaf()+"+++++++++++++++++++");
					   flg=1;
				   }
				   node2.add(new DefaultMutableTreeNode(decision1.elementAt(h)));
				   pathInd=0;
				   break;
			   }
		   }
		   //System.out.print(e1.nextElement()+">>>>>>>>>>>");
	   }
	   System.out.println("coming3");
	
 }
   
   
   
   
   
   
   
   
   
   //////////////
   
public void call(String s1,String s2)
{
	//Enumeration e=root.breadthFirstEnumeration();
	Enumeration e=root.breadthFirstEnumeration();
	while(e.hasMoreElements())
	{
		DefaultMutableTreeNode node=(DefaultMutableTreeNode)e.nextElement();
		Enumeration e1=node.pathFromAncestorEnumeration(root);
		while(e1.hasMoreElements())
		{
			System.out.print(e1.nextElement()+">>>>>>>>>>>");			
		}		
		System.out.println();
		//System.out.println(node.getUserObject()+" ..."+node.pathFromAncestorEnumeration());
		if(node.getUserObject().equals(s1))
		{
			//node.children()
			node.add(new DefaultMutableTreeNode(s2));
			System.out.println("yes");
			break;
			
		}
	}
	System.out.println("yes");
	
	}
JTree tree;
   private static final int WIDTH = 300;
   private static final int HEIGHT = 200;
   DefaultMutableTreeNode root;	

}